/*
 * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.nio.channels;

import java.io.IOException;
import java.nio.ByteBuffer;


/**
 * 一种能将字节读入缓冲区序列的通道。
 *
 * <p> 分散读操作在一次调用中将一个字节集合读入一个或多个给定的缓冲区序列中。
 * 分散读取在实现网络协议或文件格式时通常是有用的，
 * 例如，将数据分组成段，由一个或多个固定长度的报头和可变长度的主体组成。
 * 在GatheringByteChannel接口中定义了类似的收集写操作。</p>
 * 
 * 
 *
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface ScatteringByteChannel
    extends ReadableByteChannel
{

    /**
     * 从这个通道读取一个字节序列到给定缓冲区的子序列中。
     *
     * <p> 这个方法的调用尝试从这个通道读取最多r字节，
     * 其中r是保留给定缓冲区数组的指定子序列的总字节数，也就是说，
     *
     * <blockquote><pre>
     * dsts[offset].remaining()
     *     + dsts[offset+1].remaining()
     *     + ... + dsts[offset+length-1].remaining()</pre></blockquote>
     *
     * 在调用此方法时。
     *
     * <p> 假设一个字节序列长度为n被读取,0 < = n < = r。
     * 最多dsts[offset].remaining()个字节序列，被转入缓冲dsts[offset],
     * 最多dsts[offset+1].remaining()个字节序列，被转入缓冲dsts[offset+1],
     * 等等,直到整个字节数列转移到给定的缓冲区。
     * 将尽可能多的字节被转移到每个缓冲区，因此，除了最后更新的缓冲区外，
     * 每个被更新的缓冲的最终位置都保证等于该缓冲区的限制。
     *
     * <p> 此方法可以在任何时候调用。但是，如果另一个线程已经在这个通道上启动了读操作，那么对这个方法的调用将阻塞，直到第一个操作完成。</p>
     *
     * @param  dsts
     *         The buffers into which bytes are to be transferred
     *
     * @param  offset
     *         The offset within the buffer array of the first buffer into
     *         which bytes are to be transferred; must be non-negative and no
     *         larger than <tt>dsts.length</tt>
     *
     * @param  length
     *         The maximum number of buffers to be accessed; must be
     *         non-negative and no larger than
     *         <tt>dsts.length</tt>&nbsp;-&nbsp;<tt>offset</tt>
     *
     * @return The number of bytes read, possibly zero,
     *         or <tt>-1</tt> if the channel has reached end-of-stream
     *
     * @throws  IndexOutOfBoundsException
     *          If the preconditions on the <tt>offset</tt> and <tt>length</tt>
     *          parameters do not hold
     *
     * @throws  NonReadableChannelException
     *          If this channel was not opened for reading
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the read operation is in progress
     *
     * @throws  ClosedByInterruptException
     *          If another thread interrupts the current thread
     *          while the read operation is in progress, thereby
     *          closing the channel and setting the current thread's
     *          interrupt status
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public long read(ByteBuffer[] dsts, int offset, int length)
        throws IOException;

    /**
     * 从这个通道读取一个字节序列到给定的缓冲区。
     *
     * <p> 调用这种形式的c.read(dsts)方法的行为方式与调用完全相同
     *
     * <blockquote><pre>
     * c.read(dsts, 0, dsts.length);</pre></blockquote>
     *
     * @param  dsts
     *         The buffers into which bytes are to be transferred
     *
     * @return The number of bytes read, possibly zero,
     *         or <tt>-1</tt> if the channel has reached end-of-stream
     *
     * @throws  NonReadableChannelException
     *          If this channel was not opened for reading
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the read operation is in progress
     *
     * @throws  ClosedByInterruptException
     *          If another thread interrupts the current thread
     *          while the read operation is in progress, thereby
     *          closing the channel and setting the current thread's
     *          interrupt status
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public long read(ByteBuffer[] dsts) throws IOException;

}
